package schema

func (s Sets) GetValue(category, key string) *Set {
	for _, value := range s {
		if value.Key == key && value.Category == category {
			return value
		}
	}

	return nil
}

func (s Sets) SetValue(v *Set) bool {
	for _, value := range s {
		if value.Key == v.Key && value.Category == v.Category {
			value = v
			return true
		}
	}

	return false
}

func (s Sets) GetCategory(category string) map[string]string {
	list := make(map[string]string)
	for _, value := range s {
		if value.Category == category {
			list[value.Key] = value.Value
		}
	}


	return list
}

type MapSet map[string]map[string]string

func (a Sets) ToMap() MapSet {
	m := make(MapSet)

	for _, v := range a {
		if m[v.Category] == nil {
			m[v.Category] = make(map[string]string)
		}
		m[v.Category][v.Key] = v.Value
	}

	return m
}

func (a MapSet) ToSets(Type int, creator string) Sets {
	var sets Sets
	for category, kv := range a {
		for k, v := range kv {
			set := &Set{Status: 1, Creator: creator, Type: Type, Name: k, Key: k, Value: v, Category: category}
			sets = append(sets, set)
		}
	}
	return sets
}

type UploadFile struct {
	FileName string `json:"file_name"`
}